import {checkLogin, getLoginUser, toLogin} from "../../service/user";
import {goPay, prepareCacheOrder} from "../../service/order";
import {getGoodsInfo} from "../../service/goods";
import {commonShareAppMessage, commonShareTimeline} from "../../utils/share";
import goodsCartService from "../../service/goodsCart";
import {showToast} from "../../utils/tool";

const app = getApp()
const global = app.globalData

Page({
    data: {
        nav:{
            height:false,
            color:"#fff",
        },
        options:{},
        imageUrl: global.imageUrl,
        info:null,
        user:null,
        specShow:false,
    },
    onLoad: function (options = {}) {

        (async () => {
            console.log("goods-show",options)
            const info = await getGoodsInfo(options.id)
            const user = await getLoginUser()
            this.setData({
                info,
                user
            })

        })()

    },
    specPopupOpen(){
        this.setData({
            specShow:true,
        })
    },

    /**
     *   单规格商品数量变化
     */
    numChange(e){
        const {value} = e.detail
        this.setData({
            [`info.num`]: parseInt(value)
        })
    },

    /**
     * 底部加入购物车和购买按钮点击事件
     * @param e
     */
    barClick(e){

        // 没有登录，去登录页
        if (!checkLogin()){
            toLogin()
            return;
        }


        const {type} = e.detail
        const {info} = this.data

        if (info.spec_type == 2) {  // 多规格
            this.specPopupOpen()
        }else{

            if (type == 'cart'){    // 添加到购物车
                if (info.num == 0){
                    showToast("请选择数量")
                }
                goodsCartService.add({
                    goods_id: info.id,
                    goods_sku_id:0,
                    num: info.num,
                })

            }else if(type == 'buy') {   // 跳转到支付页面
                if (info.num == 0){
                    info.num = 1
                }
                goPay({
                    create_type:'goods',
                    order_type:'normal',
                    goodsUl:[info]
                })

            }
        }

    },

    /**
     * 图片预览
     */
    imagePreview: function (e) {
        let {images,index} = e.target.dataset
        this.setData({
            'imgPreview.images': images,
            'imgPreview.show': true,
            'imgPreview.index': index,
        })

    },

    /**
     * 转发
     */
    onShareAppMessage: function (res) {
        const {user, info, imageUrl} = this.data
        return commonShareAppMessage(user, {
            title: info.title,
            imageUrl: imageUrl + info.image,
            path:'/pages/goods/show',
            params:{
                id:info.id,
            },
        })
    },
    /**
     * 分享朋友圈
     */
    onShareTimeline: function () {
        const {user, info, imageUrl} = this.data
        return commonShareTimeline(user, {
            title: info.title,
            imageUrl: imageUrl + info.image,
            params: {
                id:info.id,
            }
        })
    },

});
